<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="医生id" prop="doctorId">
      <el-input v-model="dataForm.doctorId" placeholder="医生id"></el-input>
    </el-form-item>
    <el-form-item label="病人id" prop="patientId">
      <el-input v-model="dataForm.patientId" placeholder="病人id"></el-input>
    </el-form-item>
    <el-form-item label="手术内容" prop="operationContent">
      <el-input v-model="dataForm.operationContent" placeholder="手术内容"></el-input>
    </el-form-item>
    <el-form-item label="手术时间" prop="operationTime">
      <div class="block">
        <span class="demonstration"></span>
        <el-date-picker
          v-model="dataForm.operationTime"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right"
          :picker-options="pickerOptions">
        </el-date-picker>
      </div>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        },
        visible: false,
        dataForm: {
          id: 0,
          doctorId: '',
          patientId: '',
          operationContent: '',
          operationTime: ''
        },
        dataRule: {
          doctorId: [
            { required: true, message: '医生id不能为空', trigger: 'blur' }
          ],
          patientId: [
            { required: true, message: '病人id不能为空', trigger: 'blur' }
          ],
          operationContent: [
            { required: true, message: '手术内容不能为空', trigger: 'blur' }
          ],
          operationTime: [
            { required: true, message: '手术时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hos/operation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.doctorId = data.operation.doctorId
                this.dataForm.patientId = data.operation.patientId
                this.dataForm.operationContent = data.operation.operationContent
                this.dataForm.operationTime = data.operation.operationTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hos/operation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'doctorId': this.dataForm.doctorId,
                'patientId': this.dataForm.patientId,
                'operationContent': this.dataForm.operationContent,
                'operationTime': this.dataForm.operationTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
